package com.mengshu.platform.web.controller;

import com.mengshu.platform.config.properties.PlatformProperties;
import com.mengshu.platform.po.AdminUserPO;
import com.mengshu.platform.service.AdminUserService;
import com.mengshu.platform.utils.PasswordUtils;
import com.mengshu.platform.utils.StringUtils;
import com.mengshu.platform.web.req.LoginReq;
import com.mengshu.platform.web.resp.PlatformLoginUser;
import com.mengshu.token.annotation.LoginIgnore;
import com.mengshu.token.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;
import java.util.Objects;

/**
 * <p>
 *     用户登录
 * </p>
 *
 * @author liuning
 * @date 4:20 下午 2022/2/2
 */
@RestController
@RequestMapping("admin")
@RequiredArgsConstructor
public class AdminLoginController {

    public final LoginHelper loginHelper;

    private final AdminUserService adminUserService;

    private final PlatformProperties platformProperties;

    /**
     * 登录
     *
     * @param req
     * @return
     * @throws LoginException
     */
    @LoginIgnore
    @PostMapping("login")
    public String login(@RequestBody @Validated LoginReq req) throws LoginException {

        PlatformLoginUser loginUser;

        // 管理员登录
        if (Objects.equals(req.getName(), platformProperties.getLogin().getName())) {
            if (!Objects.equals(req.getPassword(), platformProperties.getLogin().getPassword())) {
                throw new LoginException("用户名或密码不正确");
            }

            loginUser = new PlatformLoginUser(1L, req.getName(), adminUserService.listPermissions());
        }

        // 普通用户登录
        else {

            final AdminUserPO user = adminUserService.getByName(req.getName());

            // 用户名不存在
            if (user == null) {
                throw new LoginException("用户名或密码不正确");
            }

            // 密码不正确
            if (!Objects.equals(user.getPassword(), PasswordUtils.SHA256(req.getPassword(), user.getSalt()))) {
                throw new LoginException("用户名或密码不正确");
            }

            // 账号不可用
            if (!user.getEnable()) {
                throw new LoginException("当前账号不可用");
            }

            loginUser = new PlatformLoginUser(user, adminUserService.getPermissions(user.getId()));
        }


        // 生成token
        String token = StringUtils.uuid();

        // 登录
        loginHelper.login(token, loginUser);

        return token;
    }

    /**
     * 获取当前登录用户信息
     *
     * @param platformLoginUser
     * @return
     */
    @PostMapping("token")
    public PlatformLoginUser token(PlatformLoginUser platformLoginUser) {
        return platformLoginUser;
    }

    /**
     * 退出登录
     */
    @PostMapping("logout")
    public void logout() {
        loginHelper.logout();
    }
}
